local ItemRegistry = require("constant.ItemRegistry")

ItemStack = {}
ItemStack.__index = ItemStack

function ItemStack:new(item_name, count)
    if ItemRegistry[item_name] == nil then
        error("Item " .. item_name .. " not found")
    end
    if count > ItemRegistry[item_name].max_count then
        error("Item " .. item_name .. " count is greater than max count")
    end

    local obj = {
        item = ItemRegistry[item_name],
        count = count,
    }
    setmetatable(obj, self)
    return obj
end

function ItemStack:add(count)
    local new_count = math.min(self.count + count, self.item.max_count)
    local added = new_count - self.count
    self.count = new_count
    return added
end

function ItemStack:remove(count)
    local new_count = math.max(self.count - count, 0)
    local removed = self.count - new_count
    self.count = new_count
    return removed
end